﻿Imports System.Linq
Imports System.Linq.Expressions

Public Class BaseController(Of TEntity As Class)
    Public Shared dbo As New TTSDataContext()

    Public Shared Function load(ByVal ID As Integer) As TEntity
        Dim table = dbo.GetTable(Of TEntity)()
        Dim mapping = dbo.Mapping.GetTable(GetType(TEntity))
        Dim pkfield = mapping.RowType.DataMembers.Where(Function(d) d.IsPrimaryKey = True).Take(1).SingleOrDefault()
        If pkfield Is Nothing Then
            Return Nothing
        End If
        Dim param = Expressions.Expression.Parameter(GetType(TEntity), "e")
        Dim predicate = Expressions.LambdaExpression.Lambda(Of Func(Of TEntity, Boolean))(Expressions.Expression.Equal(Expressions.Expression.Property(param, pkfield.Name), Expressions.Expression.Constant(ID)), param)
        Return table.SingleOrDefault(predicate)
    End Function

    Public Shared Function load() As List(Of TEntity)
        Dim query = From entity In dbo.GetTable(Of TEntity)() _
                    Select entity
        Return query.ToList()
    End Function

    Public Shared Function save(ByVal obj As TEntity) As Integer
        Dim table = dbo.GetTable(Of TEntity)()
        Dim mapping = dbo.Mapping.GetTable(GetType(TEntity))
        Dim pkfield = mapping.RowType.DataMembers.Where(Function(d) d.IsPrimaryKey = True).Take(1).SingleOrDefault()
        If pkfield Is Nothing Then
            Return Nothing
        End If
        If Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, Nothing)) = 0 Then
            table.InsertOnSubmit(obj)
        Else
            Try
                table.Attach(obj)
            Catch ex As Exception
                'Return 0
            End Try
        End If
        Try
            dbo.SubmitChanges()
        Catch ex As Exception
            Return 0
        End Try

        If dbo.ChangeConflicts.Count = 0 Then
            Return Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, Nothing))
        Else
            dbo.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepCurrentValues)
            Return 0
        End If
    End Function

    Public Shared Function delete(ByVal obj As TEntity) As Integer
        Dim table = dbo.GetTable(Of TEntity)()
        Dim mapping = dbo.Mapping.GetTable(GetType(TEntity))
        Dim pkfield = mapping.RowType.DataMembers.SingleOrDefault(Function(d) d.IsPrimaryKey = True)
        If pkfield Is Nothing Then
            Return Nothing
        End If
        table.DeleteOnSubmit(obj)
        dbo.SubmitChanges()
        If dbo.ChangeConflicts.Count = 0 Then
            Return Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, Nothing))
        Else
            dbo.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepCurrentValues)
            Return 0
        End If
    End Function

    Public Shared Function delete(ByVal ID As Integer) As Integer
        Dim table = dbo.GetTable(Of TEntity)()
        Dim mapping = dbo.Mapping.GetTable(GetType(TEntity))
        Dim pkfield = mapping.RowType.DataMembers.SingleOrDefault(Function(d) d.IsPrimaryKey = True)
        If pkfield Is Nothing Then
            Return Nothing
        End If
        Dim param = Expressions.Expression.Parameter(GetType(TEntity), "e")
        Dim predicate = Expressions.LambdaExpression.Lambda(Of Func(Of TEntity, Boolean))(Expressions.Expression.Equal(Expressions.Expression.Property(param, pkfield.Name), Expressions.Expression.Constant(ID)), param)
        Dim obj = table.SingleOrDefault(predicate)
        Return delete(obj)
    End Function
End Class
